package com.briup.srms.sms.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.srms.sms.bean.StudentDormitory;
import com.briup.srms.sms.bean.StudentDormitoryExample;
import com.briup.srms.sms.bean.extend.StudentDormitoryExtend;
import com.briup.srms.sms.dao.StudentDormitoryMapper;
import com.briup.srms.sms.dao.extend.StudentDormitoryExtendMapper;
import com.briup.srms.sms.service.IStudentDormitoryService;
import com.jacky.base.utils.CustomerException;
import com.jacky.base.utils.PageVM;

@Service
public class StudentDormitoryServiceImpl implements IStudentDormitoryService {
	
	@Autowired
	private StudentDormitoryMapper sdMapper;
	@Autowired
	private StudentDormitoryExtendMapper sdExtendMapper;
	
	@Override
	public PageVM<StudentDormitoryExtend> pageQuery(int page, int pageSize,Long studentId,Long dormitoryTypeId) {
		List<StudentDormitoryExtend> list = sdExtendMapper.query(page, pageSize,studentId,dormitoryTypeId);
		long total = sdExtendMapper.count(studentId,dormitoryTypeId);
		return new PageVM<>(page, pageSize, total, list);
	}
	
	@Override
	public void subscribeDormitory(long studentId, long dormitoryTypeId)  throws CustomerException {
		
		StudentDormitoryExample example = new StudentDormitoryExample();
		example.createCriteria().andStudentIdEqualTo(studentId);//.andDormitoryTypeIdEqualTo(dormitoryTypeId);
		List<StudentDormitory> list = sdMapper.selectByExample(example);
		if(list.size()>0) {
			throw new CustomerException("您已经预约成功了！");
		}
		
		
		StudentDormitory sd = new StudentDormitory();
		sd.setStudentId(studentId);
		sd.setDormitoryTypeId(dormitoryTypeId);
		sd.setSubscribeTime(new Date().getTime());
		sdMapper.insert(sd);
	}
}
